package com.coldtech.circlecrash.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import com.coldtech.circlecrash.Sprite;
import com.coldtech.circlecrash.SpriteSheet;
import com.coldtech.circlecrash.handler.ButtonHandler;

public class TextButton extends Button {
	private String text;

	private Color textColor;
	private Font textFont;

	public TextButton(ButtonHandler buttonHandler, String text, int x, int y,
			int width, int height) {
		super(buttonHandler, x, y, width, height);
		this.text = text;
		textColor = Color.WHITE;
		textFont = new Font("default", Font.BOLD, 14);
	}

	public void setColor(Color textColor) {
		this.textColor = textColor;
	}

	public void setFont(Font textFont) {
		this.textFont = textFont;
	}

	public void draw(Graphics2D g) {
		Sprite buttonSprite;
		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 0, 0);
		if (buttonSprite != null) {
			g.drawImage(buttonSprite.getImage(), getX(), getY(),
					buttonSprite.getWidth(), buttonSprite.getHeight(), null);
		}

		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 1, 0);
		if (buttonSprite != null) {
			for (int x = 9; x <= getWidth() - 18; x += 9) {
				g.drawImage(buttonSprite.getImage(), getX() + x, getY(),
						buttonSprite.getWidth(), buttonSprite.getHeight(), null);
			}
			int mod = (getWidth() - 9) % 9;

			g.drawImage(buttonSprite.getImage(), getX()
					+ (getWidth() - 9 - mod), getY(), mod,
					buttonSprite.getHeight(), null);
		}

		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 2, 0);
		if (buttonSprite != null) {
			g.drawImage(buttonSprite.getImage(), getX() + getWidth() - 9,
					getY(), buttonSprite.getWidth(), buttonSprite.getHeight(),
					null);
		}

		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 0, 1);
		if (buttonSprite != null) {
			for (int y = 9; y <= getHeight() - 18; y += 9) {
				g.drawImage(buttonSprite.getImage(), getX(), getY() + y,
						buttonSprite.getWidth(), buttonSprite.getHeight(), null);
			}
			int mod = (getHeight() - 9) % 9;

			g.drawImage(buttonSprite.getImage(), getX(), getY()
					+ (getHeight() - 9 - mod), buttonSprite.getWidth(), mod,
					null);
		}

		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 1, 1);
		if (buttonSprite != null) {
			int modx = (getWidth() - 9) % 9;
			int mody = (getHeight() - 9) % 9;
			for (int x = 9; x <= getWidth() - 18; x += 9) {
				for (int y = 9; y <= getHeight() - 18; y += 9) {
					g.drawImage(buttonSprite.getImage(), getX() + x,
							getY() + y, buttonSprite.getWidth(),
							buttonSprite.getHeight(), null);
				}
				g.drawImage(buttonSprite.getImage(), getX() + x, getY()
						+ (getHeight() - 9 - mody), buttonSprite.getWidth(),
						mody, null);
			}
			for (int y = 9; y <= getHeight() - 18; y += 9) {
				g.drawImage(buttonSprite.getImage(), getX()
						+ (getWidth() - 9 - modx), getY() + y, modx,
						buttonSprite.getHeight(), null);
			}
			g.drawImage(buttonSprite.getImage(), getX()
					+ (getWidth() - 9 - modx), getY()
					+ (getHeight() - 9 - mody), modx, mody, null);
		}

		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 2, 1);
		if (buttonSprite != null) {
			for (int y = 9; y <= getHeight() - 18; y += 9) {
				g.drawImage(buttonSprite.getImage(), getX() + getWidth() - 9,
						getY() + y, buttonSprite.getWidth(),
						buttonSprite.getHeight(), null);
			}
			int mod = (getHeight() - 9) % 9;

			g.drawImage(buttonSprite.getImage(), getX() + getWidth() - 9,
					getY() + (getHeight() - 9 - mod), buttonSprite.getWidth(),
					mod, null);
		}

		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 0, 2);
		if (buttonSprite != null) {
			g.drawImage(buttonSprite.getImage(), getX(), getY() + getHeight()
					- 9, buttonSprite.getWidth(), buttonSprite.getHeight(),
					null);
		}

		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 1, 2);
		if (buttonSprite != null) {
			for (int x = 9; x <= getWidth() - 18; x += 9) {
				g.drawImage(buttonSprite.getImage(), getX() + x, getY()
						+ getHeight() - 9, buttonSprite.getWidth(),
						buttonSprite.getHeight(), null);
			}
			int mod = (getWidth() - 9) % 9;

			g.drawImage(buttonSprite.getImage(), getX()
					+ (getWidth() - 9 - mod), getY() + getHeight() - 9, mod,
					buttonSprite.getHeight(), null);
		}

		buttonSprite = SpriteSheet.TEXTBUTTON.getSprite(getState() * 3 + 2, 2);
		if (buttonSprite != null) {
			g.drawImage(buttonSprite.getImage(), getX() + getWidth() - 9,
					getY() + getHeight() - 9, buttonSprite.getWidth(),
					buttonSprite.getHeight(), null);
		}

		g.setFont(textFont);
		g.setColor(textColor);

		FontMetrics fm = g.getFontMetrics(textFont);
		Rectangle2D rect = fm.getStringBounds(text, g);

		g.drawString(text, getX() + (getWidth() - ((int) (rect.getWidth())))
				/ 2, getY() + (getHeight() - ((int) (rect.getHeight()))) / 2
				+ fm.getAscent());
	}
}
